using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace SpellCore
{
    [ComDefaultInterface(typeof(ISpellCheckerRegistry))]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    [Guid("9410664C-E8C3-49B5-AB4B-39A17E5BE1FD")]
    public class SpellCheckerRegistry : ISpellCheckerRegistry
    {
        private static ISpellCheckerRegistry _singleton;
        private Dictionary<string, ISpellChecker> _registry = new Dictionary<string, ISpellChecker>();

        public static ISpellCheckerRegistry Checkers
        {
            get
            {
                if (_singleton == null)
                {
                    _singleton = new SpellCheckerRegistry();
                }
                return _singleton;
            }
        }

        public ISpellChecker this[string writingSystemId]
        {
            get
            {
                ISpellChecker spellChecker;
                if (!_registry.TryGetValue(writingSystemId, out spellChecker))
                {
                    spellChecker = new AggregatedSpellSession(writingSystemId);
                    _registry[writingSystemId] = spellChecker;
                }

                return spellChecker;
            }
        }

        public void RemoveAll()
        {
            _registry.Clear();
        }

        public void RemoveSpellCheckerFromRegistry(string writingSystemId)
        {
            _registry.Remove(writingSystemId);
        }
    }
}